D:\a\cssh-rs\cssh-rs\cssh-rs-core\src\client\dispatch.rs
Line | Count | Source |
1 | | //! Application of daemon-to-client messages. |
2 | | |
3 | | use std::io::Write; |
4 | | use std::ops::ControlFlow; |
5 | | use std::sync::Mutex; |
6 | | |
7 | | use log::{info, warn}; |
8 | | use tokio::sync::watch; |
9 | | use windows::Win32::System::Console::{CTRL_BREAK_EVENT, CTRL_C_EVENT}; |
10 | | |
11 | | use cssh_rs_protocol::v1::input::{ClientRunState, SignalKind}; |
12 | | use cssh_rs_protocol::v1::message::DaemonToClient; |
13 | | use cssh_rs_protocol::ClientState; |
14 | | |
15 | | use crate::client::input_bytes::input_event_to_bytes; |
16 | | use crate::client::pty::write_to_master; |
17 | | use crate::utils::windows::WindowsApi; |
18 | | |
19 | | /// Apply one daemon-to-client message. |
20 | | /// |
21 | | /// # Arguments |
22 | | /// |
23 | | /// * `api` - The Windows API implementation to use. |
24 | | /// * `message` - The decoded message. |
25 | | /// * `master` - PTY master writer. |
26 | | /// * `child_pid` - Process id of the SSH child, the signal target. |
27 | | /// * `state_sender` - Watch sender for the client state. |
28 | | /// * `highlight_sender` - Watch sender for the highlight flag. |
29 | | /// |
30 | | /// # Returns |
31 | | /// |
32 | | /// [`ControlFlow::Break`] when the daemon requested termination. |
33 | 9 | pub(crate) fn dispatch_daemon_message( |
34 | 9 | api: &dyn WindowsApi, |
35 | 9 | message: DaemonToClient, |
36 | 9 | master: &Mutex<impl Write>, |
37 | 9 | child_pid: u32, |
38 | 9 | state_sender: &watch::Sender<ClientState>, |
39 | 9 | highlight_sender: &watch::Sender<bool>, |
40 | 9 | ) -> ControlFlow<()> { |
41 | 9 | match message { |
42 | 1 | DaemonToClient::Input { event } => write_to_master(master, &input_event_to_bytes(&event)), |
43 | 3 | DaemonToClient::Signal(kind) => { |
44 | 3 | let ctrl_event2 = match kind { |
45 | 1 | SignalKind::Interrupt => CTRL_C_EVENT, |
46 | 1 | SignalKind::Break => CTRL_BREAK_EVENT, |
47 | 1 | SignalKind::Unknown => return ControlFlow::Continue(()), |
48 | | }; |
49 | 2 | if let Err(err0 ) = api.generate_console_ctrl_event(ctrl_event, child_pid) { |
50 | 0 | warn!("Failed to relay console control event: {}", err); |
51 | 2 | } |
52 | | } |
53 | 2 | DaemonToClient::StateChange { state } => { |
54 | 2 | state_sender.send_replace(match state { |
55 | 1 | ClientRunState::Disabled => ClientState::Disabled, |
56 | 1 | ClientRunState::Active | ClientRunState::Unknown => ClientState::Active, |
57 | | }); |
58 | | } |
59 | 1 | DaemonToClient::Highlight { on } => { |
60 | 1 | highlight_sender.send_replace(on); |
61 | 1 | } |
62 | 1 | DaemonToClient::Terminate { reason } => { |
63 | 1 | info!("Daemon requested termination: {}", reason); |
64 | 1 | return ControlFlow::Break(()); |
65 | | } |
66 | 1 | DaemonToClient::Activate { .. } | DaemonToClient::KeepAlive | DaemonToClient::Unknown => {} |
67 | | } |
68 | 7 | return ControlFlow::Continue(()); |
69 | 9 | } |
70 | | |
71 | | #[cfg(test)] |
72 | | #[path = "../tests/client/test_dispatch.rs"] |
73 | | mod tests; |